// 流程创建
import { JSONPath } from 'jsonpath-plus'
// import qs from 'qs'
function wokflowRequest (self) {
    function replaceUndefinedWithEmptyString (obj) {
      // 确保参数是一个对象
      if (obj && typeof obj === 'object') {
          // 遍历对象的所有属性
          Object.keys(obj).forEach(key => {
              // 如果属性值是对象，则递归调用
              if (obj[key] && typeof obj[key] === 'object') {
                  replaceUndefinedWithEmptyString(obj[key])
              } else if (obj[key] === undefined) {
                  // 如果属性值是 undefined，则替换为 ''
                  obj[key] = ''
              }
          })
      }
      return obj
    }
    // 这个是nodeApproval界面
    function mockOutPut (data) {
        // 自定义出参 测试的时候用这个
        try {
            const result = self.$http.post('/mock/mockOutPut', data)
            // self.$message.success('自定义出参成功')
            return result
        } catch (e) {
            // self.$message.error('自定义出参查询失败')
        }
    }
    function outputData (viewUi) {
        // 不展示测试按钮，拿表单的数据展示
        const dataIndexList = JSONPath('pageData[*].formData.dataIndex', viewUi)
        const filteredList = dataIndexList.filter(item => item !== '')
        const _form = filteredList.reduce((acc, key, index) => {
        acc[key] = filteredList[index]
        return acc
        }, {})
        self.$store.state.nodepop.outParameters = _form // 给出参赋值
    }
    async function queryParam (appConfId) {
        // 节点信息查询
        try {
            const result = await self.$http.get('/formInfo/queryParam', { params: { appConfId: appConfId } })
            return result.data
        } catch (e) {
            // console.error(e)
        }
    }
    async function saveParam (data) {
        // 保存节点
        try {
            const result = await self.$http.post('/formInfo/saveParam', replaceUndefinedWithEmptyString(data))
            return result.data
        } catch (e) {
            throw new Error('参数保存保存抛出异常!')
        }
    }

    async function queryList (data) {
        // 表单信息查询
        try {
            const result = await self.$http.post('/formInfo/queryList', data)
            // self.$message.success('表单信息查询成功')
            return result
        } catch (e) {
            // self.$message.error('表单信息查询失败')
        }
    }
    async function datasourceQuery (data) {
        // 数据源查询
        try {
            const result = await self.$http.post('/datasource/queryList', data)
            return result
        } catch (e) {
            // self.$message.error('数据源查询失败')
        }
    }
    async function viewList (data) {
        // 表单视图查询  //这个后面应该也不用了
        try {
          const result = await self.$http.post('/node/taskView/viewList', data)
          // self.$message.success('表单视图查询成功')
          return result
        } catch (e) {
          // self.$message.error('表单视图查询失败')
        }
    }
    async function viewDesc (viewId) {
      // 表单视图查询
      try {
        const result = await self.$http.get('/node/taskView/viewDesc', { params: { viewId } })
        // self.$message.success('表单视图查询成功')
        return result
      } catch (e) {
        // self.$message.error('表单视图查询失败')
      }
    }
    async function viewSave (data) { // 视图保存
      try {
        const result = await self.$http.post('/node/taskView/viewSave', data)
        return result
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryTableList (data) { // 查询
      try {
        const result = await self.$http.post('/tableInfo/queryTableList', data)
        return result
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryTableDesc (value) {
      // 查询formUi
      const heads = {
        'Content-Type': 'application/json'
      }
      try {
        const result = await self.$http.get('/tableInfo/queryTableDesc', { params: { formId: value }, headers: heads })
        if (result.code !== 200) {
          // self.$message.error('查询formUi错误')
        }
        return result
      } catch (e) {
          // self.$message.error('查询formUi错误', e)
      }
    }

    async function queryShowColumn (dataId) { // 数据源出参查询
      try {
        const result = await self.$http.get('/datasource/queryShowColumn', { params: { dataId: dataId } })
        return result.data
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryOutPutByFormId (data) {
        // 应用出参 不想测试的时候用这个 后面自动给他测试应该不用这个了
        try {
            const result = await self.$http.post('/outParam/queryOutPutByFormId', data)
            // self.$message.success('应用出参查询成功')
            return result
        } catch (e) {
            // self.$message.error('应用出参查询失败')
        }
    }

    async function processUpd (data) {
      // 流程编辑
      try {
        const result = await self.$http.post('/node/processModel/upd', data)
        // self.$message.success('流程编辑成功')
        return result
      } catch (e) {
        // self.$message.error('流程编辑失败')
      }
    }
    async function processExport (data) {
      // 流程导出
      try {
        const result = await self.$http.post('/node/processModel/processExport', data, {
          // responseType: 'blob'
          responseType: 'arraybuffer'

        })
        // self.$message.success('导出成功')
        return result
      } catch (e) {
        // self.$message.error('流程编辑失败')
      }
    }
    async function processModelDel (data) {
      // 流程删除
      try {
        const result = await self.$http.get('/node/processModel/del', { params: data })
        return result
      } catch (e) {
        // self.$message.error('流程出参查询失败')
      }
    }
    async function checkProcessDel (data) {
      // 验证流程是否在执行
      try {
        const result = await self.$http.get('/node/processModel/checkProcessDel', { params: data })
        return result
      } catch (e) {
        // self.$message.error('流程出参查询失败')
      }
    }
    async function cloneProcess (data) {
      // 流程克隆
      try {
        const result = await self.$http.get('/node/processModel/cloneProcess', { params: data })
        return result
      } catch (e) {
        // self.$message.error('流程出参查询失败')
      }
    }
    async function processcreate (data) {
      // 流程创建
      try {
        const result = await self.$http.post('/node/processModel/create', data)
        // self.$message.success('流程创建成功')
        return result
      } catch (e) {
        // self.$message.error('流程创建失败')
      }
    }
    async function processOutParamSaveOrUp (data) {
      // 流程出参新增编辑
      try {
        const result = await self.$http.post('/node/processModel/processOutParamSaveOrUp', data)
        // self.$message.success('流程出参新增编辑成功')
        return result
      } catch (e) {
        // self.$message.error('流程出参新增编辑失败')
      }
    }
    async function queryProcessOutConf (data) {
      // 流程出参查询
      try {
        const result = await self.$http.get('/node/processModel/queryProcessOutConf', { params: data })
        return result
      } catch (e) {
        // self.$message.error('流程出参查询失败')
      }
    }
    async function queryExecStatus (instanceId) {
      // 节点状态查询
      try {
        const result = await self.$http.get('/node/processrun/queryExecStatus', { params: { instanceId: instanceId } })
        return result
      } catch (e) {
        // self.$message.error('流程节点状态查询失败')
      }
    }
    async function queryByPage (data) { // 查询工作流列表
      try {
        const result = await self.$http.post('/node/processModel/queryByPage', data)
        return result
      } catch (e) {
        // self.$message.error('流程列表查询失败')
      }
    }
    async function queryInfo (data) {
      // 流出查询
      const heads = {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
      try {
        const result = await self.$http.get('/node/processModel/queryInfo', { params: data, headers: heads })
        return result
      } catch (e) {
        // self.$message.error('流程节点状态查询失败')
      }
    }
    async function processrun (data) {
      // 工作流运行
      try {
          const result = await self.$http.post('/node/processrun/executeModel', replaceUndefinedWithEmptyString(data))
          return result
      } catch (e) {
          // self.$message.error('流程执行失败')
      }
    }
    async function processCheck (data) {
      // 工作流校验
      try {
          const result = await self.$http.post('/foreignKey/processCheck', data)
          return result
      } catch (e) {
          // self.$message.error('流程执行失败')
      }
    }
    async function queryMyTaskDetail (data) {
      // 表单视图查询
      try {
        const result = await self.$http.get('/node/agencyTasks/queryMyTaskDetail', { params: data })
        return result
      } catch (e) {
        console.error('queryMyTaskDetail 请求失败:', e)
      }
    }

    async function queryCompleted (data) {
      // 当前节点前所有节点的节点信息
      try {
          const result = await self.$http.post('/node/processrun/queryCompleted', data)
          return result
      } catch (e) {
          // self.$message.error('已办节点查询失败')
      }
    }

    async function queryWorkPage (data) {
      // 查看已办
      try {
          const result = await self.$http.post('/node/processrun/queryWorkPage', data)
          return result
      } catch (e) {
          // self.$message.error('已办节点查询失败')
      }
    }
    async function nodeQueryTableList (data) {
      // 表单列表
      try {
          const result = await self.$http.post('/tableInfo/nodeQueryTableList', data)
          // self.$message.success('表单列表查询成功')
          return result
      } catch (e) {
          // self.$message.error('表单列表查询失败')
      }
    }
    async function queryNodeVariable () {
      // 节点变量查询
      try {
          const result = await self.$http.get('/node/processModel/queryNodeVariable')
          // self.$message.success('节点变量查询成功')
          return result
      } catch (e) {
          // self.$message.error('节点变量查询失败')
      }
    }
    async function querySystemVariable () {
      // 系统变量查询
      try {
          const result = await self.$http.get('/node/processModel/querySystemVariable')
          // self.$message.success('系统变量查询成功')
          return result
      } catch (e) {
          // self.$message.error('系统变量查询失败')
      }
    }
     // 查询视图
     async function queryProcessView (instanceid) {
      try {
        const result = await self.$http.post(`/node/processrun/queryProcessView/${instanceid}`)
        return result
      } catch (e) {
        // console.error(e)
      }
    }

     // 查询日志
     async function queryLogs (data) {
      try {
        const result = await self.$http.post('/node/processrun/queryLogs', data)
        return result.data
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryUserInfo () { // 临时用户查询
      try {
        const result = await self.$http.get('/buttonExec/queryUserInfo')
        return result.data
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryAllMongo (data) { // 用户组查询
      try {
        const result = await self.$http.post('/usergroup/queryAllMongo', data)
        return result.data
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryRoleList (data) { // 用户组查询
      try {
        const result = await self.$http.post('/roleinfo/queryRoleList', data)
        return result.data
      } catch (e) {
        // console.error(e)
      }
    }

    async function upWordStatus (id) {
      // 改变执行状态？
      try {
          const result = await self.$http.get('/node/processrun/upWordStatus', { params: { id: id } })
          // self.$message.success('状态改变成功')
          return result
      } catch (e) {
          // self.$message.error('状态改变失败')
      }
    }
    // 服务项快捷发起配置
    async function quick (data) {
      try {
        const result = await self.$http.post('/node/item/quick/set', data)
        return result
      } catch (e) {
        // console.error(e)
      }
    }
     // 组详情查询
     async function queryGroupByItemId (data) {
      try {
        const result = await self.$http.get('/node/item/quick/queryGroupByItemId', { params: data })
        return result
      } catch (e) {
        // console.error(e)
      }
    }
     // 快捷服务列表
     async function queryItemQuickList (data) {
      try {
        const result = await self.$http.post('/node/item/quick/queryQuickList', data)
        return result
      } catch (e) {
        // console.error(e)
      }
    }
    // 快捷服务列表
    async function queryQuickByModelId (data) {
      try {
        const result = await self.$http.get('/node/item/quick/queryQuickByModelId', { params: data })
        return result
      } catch (e) {
        // console.error(e)
      }
    }
    async function queryItemDetails (serviceItemId) {
      // 查询服务项详情
      try {
          const result = await self.$http.get('/node/item/queryItemDetails', { params: { serviceItemId: serviceItemId } })
          return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function complete (data) {
      // 查询服务项详情
      try {
          const result = await self.$http.post('/node/agencyTasks/complete', replaceUndefinedWithEmptyString(data))
          return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function queryRule () {
      // 查定时任务规则
      try {
          const result = await self.$http.post('/timer/queryRule')
          return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function addRecord (data) {
      // 新增定时任务
      try {
          const result = await self.$http.post('/timer/addRecord', data)
          return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }

    async function queryTimer () {
      // 当月定时任务提醒查询
      try {
          const result = await self.$http.get('/timer/queryTimer')
          return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function delTimer (id, type) {
      // 当月定时任务提醒删除
      try {
          const result = await self.$http.get('/timer/delTimer', { params: { id: id, type: type } })
          return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function delFuncList (data) {
      // 删除多余视图
      try {
        const result = await self.$http.post('/node/taskView/delFuncList', data)
        return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function queryView (dataId) {
      // 删除多余视图
      try {
        const result = await self.$http.get('/datasource/queryView', { params: { dataId: dataId } })
        return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function importProcess (data) {
      // 导入流程
      try {
        const result = await self.$http.post('node/processModel/importProcess', data)
        return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function queryByDbName (data) {
      // dbname查表单
      try {
        const result = await self.$http.post('tableInfo/queryByDbName', data)
        return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function importBeforeMapping (data) {
      // 上传文件
      try {
        const result = await self.$http.post('/node/processModel/importBeforeMapping', data, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function querytb (data) {
      // 查tab
      try {
        const result = await self.$http.post('/buttonExec/query/tb21801/4', data)
        return result
      } catch (e) {
          // self.$message.error('查询服务项详情失败')
      }
    }
    async function queryDesc (data) {
      // 查tab
      try {
        const result = await self.$http.get('buttonExec/edit/queryDesc/', { params: data })
        return result
      } catch (e) {
      }
    }
    // 查询数据源列表
    async function queryDatasourceList (data) {
      try {
        const result = await self.$http.post(`/datasource/querySourceAllList`, data)
        return result
      } catch (e) {
        console.error('请求失败', e)
      }
    }
    // 查询流程视图
    async function queryModelView (data) {
      try {
        const result = await self.$http.get(`/node/processModel/queryModelView`, { params: data })
        return result
      } catch (e) {
        console.error('请求失败', e)
      }
    }
    // 查询数据源详情
    async function queryDataSourceDetail (data) {
      try {
        const result = await self.$http.get(`/datasource/queryDataSourceDetail`, { params: data })
        return result
      } catch (e) {
        console.error('请求失败', e)
      }
    }
    // 团队代办
    async function claimUserGroupIds (data) {
      try {
        const result = await self.$http.post('/task/taskInfo/runExecution/claimUserGroupIds', data)
        return result
      } catch (e) {
        console.error('请求失败,但走这就对了', e)
      }
    }
    // 判断代办类型
    async function claim (data) {
      try {
        const result = await self.$http.get('/task/taskInfo/runExecution/claim', { params: data })
        return result
      } catch (e) {
        console.error('请求失败,但走这就对了', e)
      }
    }
    // 查询代办用户组
    async function chooseTaskByGroup (data) {
      try {
        const result = await self.$http.get('/task/taskInfo/runExecution/chooseTaskByGroup', { params: data })
        return result
      } catch (e) {
        console.error('请求失败,但走这就对了', e)
      }
    }
    // nodeid查团队代办视图
    async function getTeamTaskDetail (data) {
      try {
        const result = await self.$http.get(`/node/agencyTasks/getTeamTaskDetail`, { params: data })
        return result
      } catch (e) {
        console.error('请求失败', e)
      }
    }
    async function getNodeExecStatus (instanceId) {
      // 节点状态查询
      try {
        const result = await self.$http.get('/node/processrun/getNodeExecStatus', { params: { instanceId: instanceId } })
        return result
      } catch (e) {
        // self.$message.error('流程节点状态查询失败')
      }
    }
    async function dataExec (data) {
      // 查数据源
      try {
        const result = await self.$http.post('/datasource/dataExec', data)
        return result
      } catch (e) {
        // self.$message.error('流程节点状态查询失败')
      }
    }
    async function queryTeamTask (data) {
      // 查团队代办
      try {
        const result = await self.$http.post('/node/agencyTasks/queryTeamTask', data)
        return result
      } catch (e) {
        // self.$message.error('流程节点状态查询失败')
      }
    }
    async function getUpApprove (data) {
      // 查审批
      try {
        const result = await self.$http.get('/node/processrun/getUpApprove', { params: data })
        return result
      } catch (e) {
        // self.$message.error('流程节点状态查询失败')
      }
    }
    return {
        outputData,
        mockOutPut,
        // outputTestData,
        queryAllMongo,
        queryRoleList,
        queryUserInfo,
        queryParam,
        saveParam,
        queryList,
        datasourceQuery,
        viewList,
        viewDesc,
        viewSave,
        queryTableList,
        queryTableDesc,
        queryShowColumn,
        queryOutPutByFormId,
        queryNodeVariable,
        querySystemVariable,
        processUpd,
        processcreate,
        checkProcessDel,
        cloneProcess,
        queryExecStatus,
        getNodeExecStatus,
        queryInfo,
        queryMyTaskDetail,
        queryCompleted,
        complete,
        queryWorkPage,
        queryProcessView,
        queryLogs,
        queryByPage,
        processrun,
        processCheck,
        processExport,
        processModelDel,
        processOutParamSaveOrUp,
        queryProcessOutConf,
        nodeQueryTableList,
        upWordStatus,
        quick,
        queryGroupByItemId,
        queryItemQuickList,
        queryQuickByModelId,
        queryItemDetails,
        queryTimer,
        delTimer,
        delFuncList,
        queryView,
        importProcess,
        queryByDbName,
        importBeforeMapping,
        querytb,
        queryDesc,
        queryDatasourceList,
        queryModelView,
        queryDataSourceDetail,
        claimUserGroupIds,
        claim,
        chooseTaskByGroup,
        getTeamTaskDetail,
        dataExec,
        queryTeamTask,
        getUpApprove,
        queryRule,
        addRecord
    }
  }
  export default wokflowRequest
